#include "ShaderManager.h"

ShaderManager* ShaderManager::sm_pInstance = nullptr;

ShaderManager::ShaderManager(void)
{

}

ShaderManager* ShaderManager::GetInstance(void)
{
	if(!sm_pInstance)
		sm_pInstance = new ShaderManager;
	return sm_pInstance;
}

void ShaderManager::DeleteInstance(void)
{
	if(sm_pInstance)
	{
		delete sm_pInstance;
		sm_pInstance = nullptr;
	}
}

void ShaderManager::Initialize(void)
{
}

int ShaderManager::LoadShader(const char* filename)
{
	ID3DXEffect* newEffect = RENDERER->LoadEffect(filename);
	if(!newEffect)
		return -1;

	m_vpShaders.push_back(newEffect);
	return m_vpShaders.size() - 1;
}

UINT ShaderManager::BeginShader(int shaderID)
{
	if(shaderID >= 0 && shaderID < (int)m_vpShaders.size())
	{
		UINT numPasses;
		m_vpShaders[shaderID]->Begin(&numPasses, 0);
		return numPasses;
	}

	return 0;
}

void ShaderManager::EndShader(int shaderID)
{
	if(shaderID >= 0 && shaderID < (int)m_vpShaders.size())
	{
		m_vpShaders[shaderID]->End();
	}
}

void ShaderManager::BeginPass(int shaderID, UINT pass)
{
	if(shaderID >= 0 && shaderID < (int)m_vpShaders.size())
	{
		m_vpShaders[shaderID]->BeginPass(pass);
	}
}

void ShaderManager::EndPass(int shaderID)
{
	if(shaderID >= 0 && shaderID < (int)m_vpShaders.size())
	{
		m_vpShaders[shaderID]->EndPass();
	}
}

void ShaderManager::SetTechnique(int shaderID, const char* technique)
{
	if(shaderID >= 0 && shaderID < (int)m_vpShaders.size())
	{
		m_vpShaders[shaderID]->SetTechnique(technique);
	}
}

void ShaderManager::SetMatrix(int shaderID, D3DXHANDLE param, const D3DXMATRIX* matrix)
{
	if(shaderID >= 0 && shaderID < (int)m_vpShaders.size())
	{
		m_vpShaders[shaderID]->SetMatrix(param, matrix);
	}
}

void ShaderManager::SetTexture(int shaderID, D3DXHANDLE param, IDirect3DTexture9* texture)
{
	if(shaderID >= 0 && shaderID < (int)m_vpShaders.size())
	{
		m_vpShaders[shaderID]->SetTexture(param, texture);
	}
}

void ShaderManager::CommitChanges(int shaderID)
{
	if(shaderID >= 0 && shaderID < (int)m_vpShaders.size())
	{
		m_vpShaders[shaderID]->CommitChanges();
	}
}